/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.service;

import java.util.List;

import com.thinkgem.jeesite.modules.hk.adv.entity.HkAllAdv;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkMediaAdv;
import com.thinkgem.jeesite.modules.hk.sys.entity.recommend.HkLoveSeeRecommend;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUser;
import com.thinkgem.jeesite.modules.hk.sys.service.recommend.HkLoveSeeRecommendService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkEnterpriseAdv;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkEnterpriseAdvDao;

/**
 * 企业广告管理Service
 * @author 罗天文
 * @version 2018-03-20
 */
@Service
@Transactional(readOnly = true)
public class HkEnterpriseAdvService extends CrudService<HkEnterpriseAdvDao, HkEnterpriseAdv> {
	@Autowired
	HkAllAdvService hkAllAdvService;
	@Autowired
	private HkLoveSeeRecommendService hkLoveSeeRecommendService;
	public HkEnterpriseAdv get(String id) {
		return super.get(id);
	}
	
	public List<HkEnterpriseAdv> findList(HkEnterpriseAdv hkEnterpriseAdv) {
		return super.findList(hkEnterpriseAdv);
	}
	
	public Page<HkEnterpriseAdv> findPage(Page<HkEnterpriseAdv> page, HkEnterpriseAdv hkEnterpriseAdv) {
		return super.findPage(page, hkEnterpriseAdv);
	}
	
	@Transactional(readOnly = false)
	public void save(HkEnterpriseAdv hkEnterpriseAdv) {
		//hkEnterpriseAdv.setCategory(null);
		/*if(null!=hkEnterpriseAdv.getCategory()){
			hkEnterpriseAdv.setCategory(String.join(",", hkEnterpriseAdv.getCategorys()));
		}*/
		hkEnterpriseAdv.setTag(null);
		if(null!=hkEnterpriseAdv.getTags()){
			hkEnterpriseAdv.setTag(String.join(",", hkEnterpriseAdv.getTags()));
		}
		super.save(hkEnterpriseAdv);

		deleteTag(hkEnterpriseAdv);
		insertTag(hkEnterpriseAdv);
		deleteCategory(hkEnterpriseAdv);
		insertCategory(hkEnterpriseAdv);
		if(hkEnterpriseAdv.getState().equals("1")) {
			insertAllAdv(hkEnterpriseAdv);
		}

	}
	
	@Transactional(readOnly = false)
	public void delete(HkEnterpriseAdv hkEnterpriseAdv) {
		super.delete(hkEnterpriseAdv);
	}

	/**
	 * 删除标签
	 * @param hkEnterpriseAdv
	 * @return
	 */
	private void deleteCategory(HkEnterpriseAdv hkEnterpriseAdv){
		super.dao.deleteCategory(hkEnterpriseAdv);
	}
	/**
	 * 增加标签
	 * @param hkEnterpriseAdv
	 * @return
	 */
	private void insertCategory(HkEnterpriseAdv hkEnterpriseAdv){
		super.dao.insertCategory(hkEnterpriseAdv);
	}
	//同步数据
	private void insertAllAdv(HkEnterpriseAdv hkEnterpriseAdv){
		String sourceId=hkEnterpriseAdv.getId();
		HkAllAdv hkAllAdvOld =hkAllAdvService.getBySourceId(sourceId);
		if(null==hkAllAdvOld){
			HkAllAdv hkAllAdv = new HkAllAdv();
			BeanUtils.copyProperties(hkEnterpriseAdv, hkAllAdv);
			hkAllAdv.setId(null);
			hkAllAdv.setSourceId(hkEnterpriseAdv.getId());
			//类型  1自媒体 2企业广告3城市广告4传统文化s
			hkAllAdv.setType("2");
			hkAllAdvService.save(hkAllAdv);
		}
		else{
			HkAllAdv hkAllAdv =hkAllAdvOld;
			BeanUtils.copyProperties(hkEnterpriseAdv, hkAllAdv);
			hkAllAdv.setId(hkAllAdvOld.getId());
			hkAllAdvService.save(hkAllAdv);
		}

		hkAllAdvService.deleteTag(hkEnterpriseAdv.getId(),hkEnterpriseAdv.getTag());
		hkAllAdvService.insertTag(hkEnterpriseAdv.getId(),hkEnterpriseAdv.getTag());
		hkAllAdvService.deleteCategory(hkEnterpriseAdv.getId(),hkEnterpriseAdv.getCategory());
		hkAllAdvService.insertCategory(hkEnterpriseAdv.getId(),hkEnterpriseAdv.getCategory());
	}
	/**
	 * 删除标签
	 * @param hkEnterpriseAdv
	 * @return
	 */
	private void deleteTag(HkEnterpriseAdv hkEnterpriseAdv){
		super.dao.deleteTag(hkEnterpriseAdv);
	}
	/**
	 * 增加标签
	 * @param hkEnterpriseAdv
	 * @return
	 */
	private void insertTag(HkEnterpriseAdv hkEnterpriseAdv){
		super.dao.insertTag(hkEnterpriseAdv);
	}
	@Transactional(readOnly = false)
    public void recommend(HkEnterpriseAdv hkEnterpriseAdv) {
		HkAllAdv hkAllAdv=hkAllAdvService.getBySourceId(hkEnterpriseAdv.getId());
		HkLoveSeeRecommend hkLoveSeeRecommend=new HkLoveSeeRecommend();
		BeanUtils.copyProperties(hkAllAdv, hkLoveSeeRecommend);
		hkLoveSeeRecommend.setId(null);
		HkUser hkUser=new HkUser();
		hkLoveSeeRecommend.setUser(hkUser);
		hkLoveSeeRecommendService.save(hkLoveSeeRecommend);
    }
}